//工厂函数
$(function(){
    //加载所有年级数据
    initGrade();
    //加载所有学生数据
    initStudent();
    //绑定查询学生事件
    searchStudent();
    //绑定保存数据事件
    saveStudent();
})

/**
 * 读取年级数据
 */
function initGrade(){
    $.ajax({
        "url":"http://127.0.0.1:3000",
        "type": "get",
        "data":"flag=searchGrade",
        "datatype":"json",
        "success":function(res){
            res = JSON.parse(res);
            //查询条件的年级
            var fu = document.getElementById("nj");
            fu.innerHTML = "";
            var option = document.createElement("option");
            option.value = "-1";
            option.innerText = "--请选择--";
            fu.appendChild(option);
            //更新选项
            for(let item of res.data){
                var option = document.createElement("option");
                option.value = item.gradeId;
                option.innerText = item.gradeName;
                fu.appendChild(option); 
            }
            
            //维护div的年级
            var fu2 = document.getElementById("xsnj");
            fu2.innerHTML = "";
            var option2 = document.createElement("option");
            option2.value = "-1";
            option2.innerText = "--请选择--";
            fu2.appendChild(option2);
            //更新选项
            for(let item of res.data){
                var option2 = document.createElement("option");
                option2.value = item.gradeId;
                option2.innerText = item.gradeName;
                fu2.appendChild(option2); 
            }

        },
        "error":function(error){
            console.log("失败！" + error);
        }
    });
}

/**
 * 读取学生数据
 */
function initStudent(){
    $.ajax({
        "url":"http://127.0.0.1:3000",
        "type": "get",
        "data":"flag=searchStudent",
        "datatype":"json",
        "success":function(res){
            res = JSON.parse(res);
            //清除数据
            var fu = document.getElementById("sj");
            fu.innerHTML = "";
            for(let item of res.data){
                var tr = document.createElement("tr");
                tr.innerHTML=`<tr>
                                <td>${item.stuNo}</td>
                                <td>${item.stuName}</td>
                                <td>${item.gradeName}</td>
                                <td>
                                    <a href='javascript:void(0)' onclick='updateData(this)'>修改</a>
                                    <a href='javascript:void(0)' onclick='deleteData(this)'>删除</a>
                                </td>
                            </tr>`;
                fu.appendChild(tr);           
            }
        },
        "error":function(error){
            console.log("失败！" + error);
        }
    });
}

/**
 * 查询按钮点击事件：
 *    根据条件过滤学生数据
 */
function searchStudent(){
    document.getElementById("search").onclick=function(){
        //获取数据
        let xm = document.getElementById("xm").value;
        let index = document.getElementById("nj").selectedIndex;
        let nj = document.getElementById("nj").children[index].innerText;
        $.ajax({
            "url":"http://127.0.0.1:3000",
            "type": "get",
            "data":"flag=selectStudent&xm="+xm+"&nj="+nj,
            "datatype":"json",
            "success":function(res){
                res = JSON.parse(res);
                //清除数据
                var fu = document.getElementById("sj");
                fu.innerHTML = "";
                for(let item of res.data){
                    var tr = document.createElement("tr");
                    tr.innerHTML=`<tr>
                                    <td>${item.stuNo}</td>
                                    <td>${item.stuName}</td>
                                    <td>${item.gradeName}</td>
                                    <td>
                                        <a href='javascript:void(0)' onclick='updateData(this)'>修改</a>
                                        <a href='javascript:void(0)' onclick='deleteData(this)'>删除</a>
                                    </td>
                                </tr>`;
                    fu.appendChild(tr);           
                }
            },
            "error":function(error){
                console.log("失败！" + error);
            }
        });
    }
} 

/**
 * 删除超链接点击事件
 *    获取学生编号，传递到后台，后台做删除
 * @param obj 
 */
function deleteData(obj){
    //获取学号
    let xh = obj.parentNode.parentNode.children[0].innerText;
    $.ajax({
        "url":"http://127.0.0.1:3000",
        "type": "get",
        "data":"flag=deleteStudent&xh="+xh,
        "datatype":"json",
        "success":function(res){
            alert("删除成功！");
            res = JSON.parse(res);
            //清除数据
            var fu = document.getElementById("sj");
            fu.innerHTML = "";
            for(let item of res.data){
                var tr = document.createElement("tr");
                tr.innerHTML=`<tr>
                                <td>${item.stuNo}</td>
                                <td>${item.stuName}</td>
                                <td>${item.gradeName}</td>
                                <td>
                                    <a href='javascript:void(0)' onclick='updateData(this)'>修改</a>
                                    <a href='javascript:void(0)' onclick='deleteData(this)'>删除</a>
                                </td>
                            </tr>`;
                fu.appendChild(tr);           
            }
        },
        "error":function(error){
            console.log("失败！" + error);
        }
    });
}

/**
 * 修改超链接点击事件
 *    获取学生编号，传递到后台，后台指定数据并返回渲染页面
 * @param obj 
 */
function updateData(obj){
    let xh = obj.parentNode.parentNode.children[0].innerText;
    document.getElementById("wbbt").innerText="修改数据";
    document.getElementById("select").style.display="none";
    document.getElementById("modify").style.display="block";

    $.ajax({
        "url":"http://127.0.0.1:3000",
        "type": "get",
        "data":"flag=searchStuByNo&xh="+xh,
        "datatype":"json",
        "success":function(res){
            res = JSON.parse(res);
            document.getElementById("xsxh").value = res.data.stuNo;
            document.getElementById("xsxh").readOnly=true;
            document.getElementById("xsxm").value = res.data.stuName;
            var arr = document.getElementById("xsnj").children;
            for(let item of arr){
                if(item.innerText == res.data.gradeName){
                    item.selected = true;
                }
            }
        },
        "error":function(error){
            console.log("失败！" + error);
        }
    });

}

/**
 * 新增按钮点击事件
 *    将查询页面和新增页面切换显示
 */
function insertData(){
    document.getElementById("wbbt").innerText="新增数据";
    document.getElementById("select").style.display="none";
    document.getElementById("modify").style.display="block";
}

/**
 * 保存学生点击事件
 */
 function saveStudent(){
    document.getElementById("xsbc").onclick=function(){
        //获取数据
        let xsxh = document.getElementById("xsxh").value;
        let xsxm = document.getElementById("xsxm").value;
        let njIndex = document.getElementById("xsnj").selectedIndex;
        let xsnj = document.getElementById("xsnj").children[njIndex].innerText;
        //封装数据
        let xsxx = {
            "stuNo": xsxh,
            "stuName": xsxm,
            "gradeName": xsnj
        }
        //区分新增还是修改
        let qf = document.getElementById("wbbt").innerText;
        if(qf == "修改数据"){
            $.ajax({
                "url":"http://127.0.0.1:3000",
                "type": "get",
                "data":"flag=updateSaveStudent&stu="+JSON.stringify(xsxx),
                "datatype":"json",
                "success":function(res){
                    alert("修改成功！");
                    res = JSON.parse(res);
                    //清除数据
                    var fu = document.getElementById("sj");
                    fu.innerHTML = "";
                    for(let item of res.data){
                        var tr = document.createElement("tr");
                        tr.innerHTML=`<tr>
                                        <td>${item.stuNo}</td>
                                        <td>${item.stuName}</td>
                                        <td>${item.gradeName}</td>
                                        <td>
                                            <a href='javascript:void(0)' onclick='updateData(this)'>修改</a>
                                            <a href='javascript:void(0)' onclick='deleteData(this)'>删除</a>
                                        </td>
                                    </tr>`;
                        fu.appendChild(tr);           
                    }
    
                },
                "error":function(error){
                    console.log("失败！" + error);
                }
            });
        }else if(qf == "新增数据"){
            $.ajax({
                "url":"http://127.0.0.1:3000",
                "type": "get",
                "data":"flag=insertSaveStudent&stu="+JSON.stringify(xsxx),
                "datatype":"json",
                "success":function(res){
                    alert("新增成功！");
                    res = JSON.parse(res);
                    //清除数据
                    var fu = document.getElementById("sj");
                    fu.innerHTML = "";
                    for(let item of res.data){
                        var tr = document.createElement("tr");
                        tr.innerHTML=`<tr>
                                        <td>${item.stuNo}</td>
                                        <td>${item.stuName}</td>
                                        <td>${item.gradeName}</td>
                                        <td>
                                            <a href='javascript:void(0)' onclick='updateData(this)'>修改</a>
                                            <a href='javascript:void(0)' onclick='deleteData(this)'>删除</a>
                                        </td>
                                    </tr>`;
                        fu.appendChild(tr);           
                    }
                },
                "error":function(error){
                    console.log("失败！" + error);
                }
            });
        }
        
        document.getElementById("select").style.display="block";
        document.getElementById("modify").style.display="none";
    }
}